package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.guoguojie.dao.CommentDao;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class CommentDaoImpl extends SqlSessionDaoSupport implements CommentDao {
    private static Logger logger = LoggerFactory.getLogger(CommentDaoImpl.class);

    public void insert(Comment comment) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.CommentMapper.insertComment", comment);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Write database failure.", e);
            }
            throw new AppBusinessException("Write database failure.", e);
        }
    }

    public void updateComment(Comment comment) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CommentMapper.updateComment", comment);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("update contact failure.", e);
            }
            throw new AppBusinessException("update contact failure.", e);
        }
    }

    public void deleteCommentById(String id) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.CommentMapper.deleteCommentById", id);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteLearning failure.", e);
            }
            throw new AppBusinessException("deleteLearning failure.", e);
        }
    }

    @Override
    public Comment getCommentById(String id) throws AppBusinessException {
        try {
            return (Comment) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentById", id);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteLearning failure.", e);
            }
            throw new AppBusinessException("deleteLearning failure.", e);
        }
    }

    @Override
    public int getCommentCountList(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentCountList", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getBlogCountList failure.", e);
            }
            throw new AppBusinessException("getCommentCountList failure.", e);
        }
    }

    @Override
    public int getCommentByOwnerTypeCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentByOwnerTypeCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    //根据条件查询评论列表的长度
    @Override
    public int getCommentCountByBlogId(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentCountByBlogId", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCommentCountByBlogId database failure.", e);
            }
            throw new AppBusinessException("getCommentCountByBlogId database failure.", e);
        }
    }

    //根据BlogId集合批量屏蔽评论
    @Override
    public void updateByCommentIdList(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CommentMapper.updateByCommentIdList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateByCommentIdList database failure.", e);
            }
            throw new AppBusinessException("updateByCommentIdList database failure.", e);
        }
    }

    //根据BlogId集合批量删除评论
    @Override
    public void deleteByCommentIdList(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CommentMapper.deleteByCommentIdList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteByCommentIdList database failure.", e);
            }
            throw new AppBusinessException("deleteByCommentIdList database failure.", e);
        }
    }

    //根据条件批量删除评论(参数1.id:commentId 2.type:comment_id_type[可选])
    @Override
    public void deleteCommentByCondition(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CommentMapper.deleteCommentByCondition", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteCommentByCondition database failure.", e);
            }
            throw new AppBusinessException("deleteCommentByCondition database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Comment> getCommentByDoingId(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Comment>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentByDoingId", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCommentByDoingId database failure.", e);
            }
            throw new AppBusinessException("getCommentByDoingId database failure.", e);
        }
    }

    @Override
    public void deleteCommentByCommentId(String commentId) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.CommentMapper.deleteCommentByCommentId", commentId);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteCommentByCommentId database failure.", e);
            }
            throw new AppBusinessException("deleteCommentByCommentId database failure.", e);
        }

    }

    @Override
    public int getCommentByDoingIdCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentByDoingIdCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCommentByDoingIdCount database failure.", e);
            }
            throw new AppBusinessException("getCommentByDoingIdCount database failure.", e);
        }
    }

    @Override
    public int getMyComCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getMyComCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMyComCount failure.", e);
            }
            throw new AppBusinessException("getMyComCount failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Comment> getCommentByDiscuss(Map<String, Object> map) throws AppBusinessException { 
        try {
            return (List<Comment>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentByDiscuss", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCommentByDiscuss database failure.", e);
            }
            throw new AppBusinessException("getCommentByDiscuss database failure.", e);
        }
    }
    
    @Override
    public int getCountByDiscuss(Map<String, Object> map) throws AppBusinessException { 
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCountByDiscuss", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getCountByDiscuss failure.", e);
            }
            throw new AppBusinessException("getCountByDiscuss failure.", e);
        }
    }

}
